package view.GUI.adminView.coreData.product;

import org.controlsfx.validation.Validator;

import view.GUI.adminView.coreData.AdminDataManaging;
import view.GUI.mainView.KindVBox.BoxType;
import view.GUI.mainView.MainView;
import view.Validator.DoubleNumberRangeValidator;
import view.Validator.IntegerNumberRangeValidator;
import view.Validator.MyValidationSupport;
import model.data.tableModel.sale.Product;
import enums.GUIText;
import enums.Message;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;


public abstract class AbstractProductView extends AdminDataManaging{

	private Label lblName;
	private Label lblCost;
	private Label lblSalePrice;
	private Label lblCategory;
	private Label lblMultiplicator;
	private Label lblStock;
	private Label lblMaxStock;
	private Label lblDeposit;
	private Label lblTrayCount;
	
	protected TextField txtName;
	protected TextField txtCost;
	protected TextField txtMultiplicator;
	protected TextField txtStock;
	protected TextField txtMaxStock;
	protected TextField txtSalePrice;
	protected TextField txtTrayCount;
	
	protected ComboBox<BoxType> cmbCategory;
	protected ComboBox<Product> cmbDeposit;	
	
	public AbstractProductView(boolean isEdit, MainView instance, GUIText title) {
		super(isEdit, 13, instance, title);
		createMyWidgets();
		addMyWidgets();
		setupMyInteraction();
		setupMyValidation();
	}


	private void setupMyValidation() {
		valSup.registerValidator(txtCost, true, DoubleNumberRangeValidator.createMinNumberRangeValidator(0.00));
		valSup.registerValidator(txtSalePrice, true, DoubleNumberRangeValidator.createMinNumberRangeValidator(0.00));
		valSup.registerValidator(txtMultiplicator, true, IntegerNumberRangeValidator.createMinNumberRangeValidator(0));
		valSup.registerValidator(txtStock, true, IntegerNumberRangeValidator.createMinNumberRangeValidator(0));
		valSup.registerValidator(txtMaxStock, true, IntegerNumberRangeValidator.createMinNumberRangeValidator(0));
		valSup.registerValidator(txtTrayCount, true, IntegerNumberRangeValidator.createMinNumberRangeValidator(0));
		valSup.registerValidator(cmbCategory, true, Validator.createEmptyValidator(Message.THISISNEEDEDINPUT.toString()));
	}


	private void addMyWidgets() {
		int rowCounter = 3;
		
		addNode(lblName, 0, rowCounter);
		addNode(txtName, 1, rowCounter++);
		
		addNode(lblCategory, 0, rowCounter);
		addNode(cmbCategory, 1, rowCounter++);
		
		addNode(lblCost, 0, rowCounter);
		addNode(txtCost, 1, rowCounter++);
		
		addNode(lblSalePrice, 0, rowCounter);
		addNode(txtSalePrice, 1, rowCounter++);
		
		addNode(lblMultiplicator, 0, rowCounter);
		addNode(txtMultiplicator,1,rowCounter++);
		
		addNode(lblStock, 0, rowCounter);
		addNode(txtStock, 1, rowCounter++);
		
		addNode(lblTrayCount, 0, rowCounter);
		addNode(txtTrayCount, 1, rowCounter++);
		
		addNode(lblMaxStock, 0, rowCounter);
		addNode(txtMaxStock, 1, rowCounter++);
		
		addNode(lblDeposit, 0, rowCounter);
		addNode(cmbDeposit, 1, rowCounter++);
	}
	
	@Override
	protected void addNode(Node node, int column, int row){
		if(node instanceof TextField){
			((TextField) node).textProperty().addListener(new ChangeListener<String>(){

				@Override
				public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
					btnSave.setDisable(MyValidationSupport.isNodeToDisable(valSup));
				}
			});
		} else if(node instanceof Pane){
			for(Node n: ((Pane) node).getChildren()){
				if(n instanceof TextField){
					((TextField) n).textProperty().addListener(new ChangeListener<String>(){

						@Override
						public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
							btnSave.setDisable(MyValidationSupport.isNodeToDisable(valSup));
						}
					});
				}
			}
		}
		super.addNode(node, column, row);
	}
	
	private void setupMyInteraction(){
		cmbCategory.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<BoxType>(){

			@Override
			public void changed(ObservableValue<? extends BoxType> observable, BoxType oldValue, BoxType newValue) {
				btnSave.setDisable(MyValidationSupport.isNodeToDisable(valSup));
			}
		});
		cmbDeposit.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Product>(){

			@Override
			public void changed(ObservableValue<? extends Product> observable, Product oldValue, Product newValue) {
				btnSave.setDisable(MyValidationSupport.isNodeToDisable(valSup));
				if(newValue != null){
					txtMultiplicator.setText(String.valueOf(newValue.getMultiplicator()));					
				}
				txtMultiplicator.setDisable(true);
			}
		});
	}


	private void createMyWidgets() {
		lblName = new Label(GUIText.NAME.toString());
		lblCost= new Label(GUIText.COST.toString());
		lblSalePrice= new Label(GUIText.SALEPRICE.toString());
		lblCategory= new Label(GUIText.CATEGORY.toString());
		lblMultiplicator= new Label(GUIText.MULTIPLICATOR.toString());
		lblStock= new Label(GUIText.STOCK.toString());
		lblMaxStock= new Label(GUIText.MAXSTOCK.toString());
		lblDeposit= new Label(GUIText.DEPOSIT.toString());
		lblTrayCount = new Label(GUIText.TRAYCOUNT.toString());
		
		txtSalePrice = new TextField();
		txtName = new TextField();
		txtCost = new TextField();
		txtMultiplicator = new TextField();
		txtStock = new TextField();
		txtMaxStock = new TextField();
		txtTrayCount = new TextField();
		
		cmbDeposit = new ComboBox<>();
		cmbDeposit.getItems().addAll(owner.getProducts(false, BoxType.DEPOSIT));
		
		cmbCategory = new ComboBox<>();
		cmbCategory.getItems().addAll(BoxType.values());
		
		cmbCategory.getSelectionModel().selectFirst();
	}
}
